﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.SqlClient;

using CapaNegocio;
using Entidades;

using Util;
namespace CapaPresentacion
{
    public partial class Carrito : System.Web.UI.Page
    {
        Pedido ped = new Pedido();
        PedidoNE pdne = new PedidoNE();

        List<DetallePedido> listaDetPedido = new List<DetallePedido>();
        DetallePedidoNE dpne = new DetallePedidoNE();

        sisReutilizable letras = new sisReutilizable();

        int idCliente = 1;
        int idEmpleado = 1;

        int numPedido;

        public double totalCarrito(DataTable dt)
        {
            double tot = 0;
            foreach (DataRow item in dt.Rows)
            {
                tot += Convert.ToDouble(item[4]);
            }
            return tot;

        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                gvCarrito.DataSource = Session["pedido"];
                gvCarrito.DataBind();
            }                       
        }

        protected void btnVerDatalle_Click(object sender, EventArgs e)
        {
            int i;
            double total = 0, prec, subtotal;
            string cod, des, p;
            int cant=2;

            var items = (DataTable)Session["pedido"];
            for (i = 0; i < gvCarrito.Rows.Count; i++)
            {
                cod = (gvCarrito.Rows[i].Cells[0].Text);
                des = (gvCarrito.Rows[i].Cells[1].Text);
                p = gvCarrito.Rows[i].Cells[2].Text;
                cant = System.Convert.ToInt32(((TextBox)
                        this.gvCarrito.Rows[i].Cells[3].FindControl("textBox1")).Text);
                prec = System.Convert.ToDouble(p);
                subtotal = cant * prec;
                gvCarrito.Rows[i].Cells[4].Text = subtotal.ToString();
                foreach (DataRow dr in items.Rows)
                {
                    if (dr["Codigo"].ToString() == cod.ToString())
                    {
                        dr["Cantidad"] = cant;
                        dr["Total"] = subtotal;
                        break;
                    }
                }
                total = total + subtotal;
            }
            float igv, totPedido;
            igv = float.Parse((total * 0.18).ToString());
            totPedido = float.Parse((igv + total).ToString());

            lblTotal.Text = total.ToString("0.00");
            lblLetras.Text = "SON " + letras.toText(double.Parse(totPedido.ToString()));
            lblLetras.Text = ("SON: "
                            + (letras.toText(double.Parse(totPedido.ToString())) + (" CON "
                            + (lblTotal.Text.Substring((lblTotal.Text.Length - 2)) + "/100 NUEVO SOLES"))));
            //lblTotal.Text = "El monto total es: S/. " + totalCarrito(items).ToString();
            //lblTotal.Text = Session["prueba"].ToString();
                
                
        }
        public void guardarPedido()
        {
            ped.IdPedido = pdne.codPedido();
            numPedido = pdne.codPedido();
            ped.IdCliente = idCliente;
            ped.IdEmpleado = idEmpleado;
            ped.FechaPedido = DateTime.Now;
            ped.SubTotal = float.Parse(lblTotal.Text);
            ped.IGV = float.Parse(Math.Round((ped.SubTotal * float.Parse("0.18")), 2).ToString());
            ped.Total = float.Parse(Math.Round((ped.SubTotal * (1 + float.Parse("0.18"))), 2).ToString());

            string rptap = pdne.insertarPedido(ped);

            int codDetalle = dpne.codDetPedido();
            for (int i = 0; i < gvCarrito.Rows.Count; i++)
            {
                DetallePedido detalle = new DetallePedido();
                detalle.IdDetalle = codDetalle;
                detalle.IdPedido = numPedido;
                detalle.IdProducto = int.Parse(gvCarrito.Rows[i].Cells[0].Text);
                detalle.Cantidad = System.Convert.ToInt32(((TextBox)
                                   this.gvCarrito.Rows[i].Cells[3].FindControl("textbox1")).Text);
                detalle.PrecioVenta = float.Parse(gvCarrito.Rows[i].Cells[2].Text);
                detalle.Descuento = float.Parse("0.05") * float.Parse(gvCarrito.Rows[i].Cells[2].Text);
                detalle.Importe = detalle.Cantidad * detalle.PrecioVenta - detalle.Descuento;
                listaDetPedido.Add(detalle);
                codDetalle++;
            }
            string rptadp = dpne.insertarDetalle(listaDetPedido);
            lblOperacion.Text = "Gracias Pedido guardado: " + rptap + "  Detalle Guardado :" + rptadp;
        }

        protected void btnReservar_Click(object sender, EventArgs e)
        {
            guardarPedido();
            gvPrueba.DataSource = listaDetPedido;
            gvPrueba.DataBind();

            Session.Remove("pedido");
        }

        protected void gvCarrito_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int n = e.RowIndex;
            DataTable ocar = new DataTable();
            ocar = (DataTable)Session["pedido"];
            ocar.Rows[n].Delete();
            lblTotal.Text = totalCarrito(ocar).ToString();

            gvCarrito.DataSource = ocar;
            gvCarrito.DataBind();
        }

        protected void gvCarrito_RowCommand(object sender, GridViewCommandEventArgs e)
        {

        }

        protected void btnComprar_Click(object sender, ImageClickEventArgs e)
        {
            guardarPedido();
            gvPrueba.DataSource = listaDetPedido;
            gvPrueba.DataBind();

            Session.Remove("pedido");
        }

        protected void gvCarrito_RowCommand1(object sender, GridViewCommandEventArgs e)
        {

        }

        protected void gvCarrito_RowDeleting1(object sender, GridViewDeleteEventArgs e)
        {
            int n = e.RowIndex;
            DataTable ocar = new DataTable();
            ocar = (DataTable)Session["pedido"];
            ocar.Rows[n].Delete();
            lblTotal.Text = totalCarrito(ocar).ToString();

            gvCarrito.DataSource = ocar;
            gvCarrito.DataBind();
        }
    }
}